package org.jeecg.modules.zhima.portal.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.jeecg.modules.zhima.center.entity.OmsCartItem;
import org.jeecg.modules.zhima.center.service.IOmsCartItemService;
import org.jeecg.modules.zhima.portal.domain.CartItem;
import org.jeecg.modules.zhima.portal.domain.PmsPortalProductDetail;
import org.jeecg.modules.zhima.portal.domain.SkuStock;
import org.jeecg.modules.zhima.portal.service.ICartService;
import org.jeecg.modules.zhima.portal.service.IProductService;
import org.jeecg.modules.zhima.portal.service.ISkuStockService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CartServiceImpl implements ICartService {

    @Autowired
    private IOmsCartItemService cartItemService;
    @Autowired
    private IProductService productService;
    @Autowired
    private ISkuStockService skuStockService;
    
    @Override
    public List<CartItem> list(Integer memberId) {
        List<CartItem> cartItems = new ArrayList<>();
        List<OmsCartItem> cartItemList = cartItemService.list(memberId);
        if (CollectionUtils.isNotEmpty(cartItemList)){
            for (int i = 0; i < cartItemList.size(); i++) {
                CartItem cartItem = new CartItem();
                OmsCartItem item = cartItemList.get(i);
                BeanUtils.copyProperties(item, cartItem);
                PmsPortalProductDetail productDetail = productService.getDetail(item.getProductId());
                cartItem.setProduct(productDetail);
                SkuStock skuStock = skuStockService.getByProductSkuId(item.getProductSkuId());
                cartItem.setSku(skuStock);
                cartItems.add(cartItem);
            }
        }
        return cartItems;
    }

}
